package com.whh.tq;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.whh.tq.bean.Favorite;
import com.whh.tq.common.AndroidHelper;
import com.whh.tq.common.ResContentHelper;
import com.whh.tq.service.FavoriteService;
import com.whh.tq.service.FavoriteServiceImpl;

public class FavoriteActivity extends Activity {
	private ListView listFavorite;
	private List<Favorite> list;
	private FavoriteAdapter favoriteAdapter;
	private FavoriteService favoriteService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favorite);
		
		listFavorite = (ListView)findViewById(R.id.list_favorite);
		favoriteService = new FavoriteServiceImpl(getApplicationContext());
		list = favoriteService.getAll();
		favoriteAdapter = new FavoriteAdapter();
		listFavorite.setAdapter(favoriteAdapter);
		
		listFavorite.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
				TextView textFavorite = (TextView)view.findViewById(R.id.text_favorite);
				String fav = textFavorite.getText().toString();
				Calendar calendar = Calendar.getInstance();
				Date time=calendar.getTime(); 
				String date = AndroidHelper.DATE_FORMAT.format(time);
				if(ResContentHelper.isChineseStr(fav)){
					Intent intent = new Intent();
					intent.setClass(FavoriteActivity.this,StationResultActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("station", fav);
					bundle.putString("date", date);
					intent.putExtras(bundle);
					startActivity(intent);					
				}else{
					Intent intent = new Intent();
					intent.setClass(FavoriteActivity.this,TrainCodeResultActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("trainCode", fav);
					bundle.putString("date", date);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			}
		});
	}
	
	
	private class FavoriteAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View view, ViewGroup parent) {
			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_favorite, null);
			Favorite fav = list.get(position);
			String favStr = "";
			String trainCode = fav.getTrainCode();
			String station = fav.getStation();
			favStr = trainCode;
			if(null==trainCode||trainCode.equals("")){
				favStr = station;
			}
			TextView textFavorite = (TextView) view.findViewById(R.id.text_favorite);
			textFavorite.setText(favStr);
			return view;
		}

	}
}
